package com.nd.qa.shareplate.script.macaca;

/**
 * 创建方法体
 * @author huangjian
 *
 */
public class CreateMacacaCase {
	/**
	 * 创建执行脚本方法体
	 * @param caseName
	 * @param content
	 * @return
	 */
	public Object createCase(String caseName, StringBuilder content) {
		StringBuffer buffer = new StringBuffer("");
		buffer.append("TestCases testCase = new TestCases();\n");
		buffer.append("SysConstant.TOTAL_CASE_COUNT++;\n");
		buffer.append("int stepId = 0;\n");
		buffer.append("String method = Thread.currentThread() .getStackTrace()[1].getMethodName();\n");
		buffer.append("testCase.setCaseName(method);\n");
		buffer.append("testCase.setClassId(classId);\n");
		buffer.append("testCase.setCreateTime(DateUtil.getSimpleDateFormat());\n");
		buffer.append("int caseId = handle.insert(testCase);\n");
		content.append("int failNum = handle.queryCount(caseId);");
		content.append("testCase.setCaseId(caseId);\n");
		content.append("testCase.setStepCount(SysConstant.TOTAL_STEP_COUNT);\n");
		content.append("testCase.setCaseErrors(SysConstant.FAIL_STEP_COUNT);\n");
		content.append("testCase.setCaseFailures(failNum);\n");
		content.append("testCase.setEndTime(DateUtil.getSimpleDateFormat());\n");
		content.append("testCase.setSpendTime(DateUtil.getDiffTime(testCase.getEndTime(),testCase.getCreateTime()));\n");
		content.append("testCase.setSuccessRate((double)(SysConstant.TOTAL_STEP_COUNT-failNum)/(SysConstant.TOTAL_STEP_COUNT));\n");
		content.append("handle.update(testCase);\n");
		content.append("SysConstant.TOTAL_STEP_COUNT=0;\n");
		content.append("SysConstant.FAIL_STEP_COUNT=0;\n");
		StringBuilder sb = new StringBuilder();
		
		sb.append(String.format("@Test\n   public void test_%s() throws Exception {\n%s\n%s}\n",caseName,buffer.toString(), content.toString()));
		return sb.toString();
	}
}
